(c) 2017 by Barton Paul Levenson
Here is a simple Fortran program:
program greetUser character(40) :: user write (*, 10) 10 format("Hi! What's your name? =>", $) read (*, *) user write (*, 20) trim(user) 20 format('Hi, ', a, '!') end program greetUser
Here's what it looks like in the Plato IDE. Note the color-coding of keywords, string literals, and so on:
The first line is a "program header," which is just the word program, and a name the programmer chooses.
The next line declares a "variable"--a data holder that can change its value. Its name is user, and it consists of a string of forty alphanumeric characters.
The third line is blank. Blank lines can go anywhere for readability.
The next couple of lines print a prompt to the screen, asking the user for his or her name. The write command takes two arguments--the symbols in parentheses following write. The first is the "unit number," which sets what device or file gets the output. An asterisk means the console; the printing will come out on the screen. Unless we are working with a file, the unit for the read and write commands will always be an asterisk.
The second argument (the label 10) shows where to find the format statement for the write command. This lists the "string literals" (character sequences desired), if any, and provides fields for variables. No fields are needed yet.
The apostrophe ( ' ) marks string boundaries. To include one in a string, you have to either put two in a row, or use double quotes ( " ) for delimiters instead. Otherwise the compiler would think you wanted to end the string there, and would flag the rest of the format statement as a syntax error.
The dollar sign ( $ ) after the second parameter means the cursor should stay on the current line, rather than skipping to the next line when done printing. This is a Fortran-77 standard still available in most Fortran-95 compilers. The new standard is to include the phrase, advance='no'. Anyone who thinks this is an improvement raise their hand.
The read command has asterisks for both unit number and format. This shows you can sometimes write things out, or read them in, without formatting. Here, we simply tell the program to read in the variable, user.
user was declared with a length of 40 characters. It can hold that many, but it can also hold shorter strings of any length from 0 to 39. These would be automatically padded on the right with blanks. That's why, when we print the reply out further down, we cut off trailing blanks with the trim¬ function. This is an "intrinsic function," one the language comes with.
The last two lines of code are another write and format. This time the write command has a variable name (user) trailing it, so we're writing out the value of user--first trimming it so we don't get 40 characters in that field.
The format statement has two string literals, 'Hi, ' and '!'. In between, it has a letter a. This is a "format descriptor." It provides a field for alphanumeric (character) data. Because the a isn't followed by a number for the field width, the write statement will adjust what is printed out to exactly the length of the string corresponding to the a format.
Having written this program in the Plato IDE, we can put it together using keystrokes:
Or, you can do all three at once by clicking the blue, right-facing triangle in the toolbar at the top of the screen:
If you get any syntax errors in the first step, the build will halt until you go back and fix them.
When the program starts, it first displays an annoying little window about how it was compiled with Salford Fortran Personal Edition, and is not licensed for commercial or research use. This is a way to prompt the user to buy the full product, which has no annoying little window but costs as much as a major appliance. I'd rather put up with the annoying message than pay that much for a compiler.
The user sees
What's your name? =>
on the console screen, with the text cursor left blinking just after the arrow.
There's only a "screen," rather than a window, when you're running a Fortran program from the command prompt. When you run a Fortran compiler like this one under Windows--or some Mac or Unix graphical interface--it creates a little command-prompt window where your dialogue with the program takes place. Or you can actually boot to the command prompt, or call the Windows command prompt utility and run the program there.
You type in your answer, and the program reads it in. It then greets you by name. If the user typed Sheila in response to the What is your name? => prompt, she will now see
Hi, Sheila!
on the screen. Then the console will display
Press RETURN to close window . . .
This is because interactive Fortran originally ran on big "minicomputers" which had a [RETURN] key on the keyboard instead of an [Enter] key. Just hit [Enter], and the program will end. The console (command window) will blink out as if it never existed.
You can add comments to a program after an exclamation mark ( ! ). But an exclamation mark inside a string literal will just display along with the rest of the string when the string prints. A comment can follow a line of source code, or can occupy a line by itself.
I like to start off program modules with a comment explaining what the program or module does. The first program is repeated here, but with a little documentation added:
! greetUser greets the user by name. program greetUser character(40) :: name ! User's first name. write (*, 10) 10 format('Hi! What''s your name? =>', $) read (*, *) name write (*, 20) trim(name) 20 format('Hi, ', a, '!') end program greetUser
For the advanced student, my program as written thus far contains a logic error, although one you won't see very often. Note the distinction between two types of programming errors:
For extra credit, see if you can discover what this program's logic problem is. Note that a program can work right most of the time and only fail in rare instances. This is why you have to test all reasonable possibilities before assuming your program will do what you want it to.
Page created: | 05/05/2017 |
Last modified: | 05/05/2017 |
Author: | BPL |